package com.kedacom.ctsp.authz.security.provider;

import com.kedacom.ctsp.authz.entity.Authentication;
import com.kedacom.ctsp.authz.token.TokenInformation;
import org.springframework.security.core.session.SessionInformation;

import java.util.Date;

/**
 * 自定义SessionInformation, 用于实现UserToken
 *
 * @author xuwei
 * @create 2017-12-01 15:44
 **/
public class AuthzSessionInformation extends SessionInformation implements TokenInformation {

    private Date firstRequest;

    public AuthzSessionInformation(Object principal, String sessionId, Date lastRequest) {
        super(principal, sessionId, lastRequest);
        this.firstRequest = lastRequest;
    }

    private AuthUserDetails getUser() {
        return ((AuthUserDetails) super.getPrincipal());
    }

    @Override
    public Authentication getPrincipal() {
        return getUser().getAuthentication();
    }

    @Override
    public String getToken() {
        return getSessionId();
    }

    @Override
    public long getLastRequestTime() {
        return getLastRequest().getTime();
    }

    @Override
    public long getSignInTime() {
        return firstRequest.getTime();
    }

}
